﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Truextend.CoffeShop.Service;
using Truextend.CoffeShop.Model;
using System.Data;

namespace Truextend.CoffeShop.Presenter.Orders
{
    public class ApproveOrRejectOrderPresenter 
    {
        IApproveOrRejectOrder view;

        public ApproveOrRejectOrderPresenter(IApproveOrRejectOrder view) {
            this.view = view;
        }

        public void FillView(){

            RestaurantService restaurantService = new RestaurantService();
            OrderService orderService = new OrderService();

            Restaurant restaurant = restaurantService.GetRestaurantByOrderId(view.orderId);
            Order order = orderService.GetOrderById(view.orderId);

            view.restaurantName = restaurant.Name;
            view.address = restaurant.Address;
            view.phones = restaurant.Phone1 + " - " + restaurant.Phone2;
            view.orderTitle = order.Title;

            FillOptions();
        }

        public void FillOptions()
        {
            DetailService detailService = new DetailService();
            ConsumptionService consumptionService = new ConsumptionService();
            float totalPrice = 0;

            IList<Detail> listDetails = detailService.GetAllDetailsOfAnOrder(view.orderId);

            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("OptionID");
            dataTable.Columns.Add("Description");
            dataTable.Columns.Add("Amount");
            dataTable.Columns.Add("Price");            

            foreach(Detail detail in listDetails){

                DataRow row = dataTable.NewRow();

                float price = 0;
                int amount = 0;

                IList<Consumption> listConsumption = consumptionService.GetAllConsumptionsOfADetail(detail.DetailId);

                try {
                    

                    foreach (var consumption in listConsumption)
                    {
                        price += (float)consumption.Price;
                        amount = (int)consumption.Quantity;
                    }
                
                }catch(Exception){}
                

                row["OptionID"] = detail.DetailId;
                row["Description"] = detail.Product.Name;
                row["Amount"] = amount;
                row["Price"] = price;

                totalPrice += price;
                dataTable.Rows.Add(row);
            }

            view.totalPrice = totalPrice;
            view.options = dataTable;
        }

        public void DeleteDetailOfAnOrder(int detailId){

            DetailService detailService = new DetailService();
            detailService.DeleteDetailOfAnOrder(detailId);

        }

        public void ApproveOrder() 
        {
            ChangeStateOrder("APP");
        }

        public void RejectedOrder()
        {
            ChangeStateOrder("REJ");
        }

        private void ChangeStateOrder(string state) {

            OrderService orderService = new OrderService();

            Order order = orderService.GetOrderById(view.orderId);

            orderService.ChangeStateOfAnOrder(order, state);

            if (state == "REJ")
            {
                orderService.DeleteOrder(order);
            }

        }
    }
}
